package com.cocoiko;

import com.cocoiko.client.ClientUtils;
import com.cocoiko.utils.CocoikoUtils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Login extends Activity implements OnClickListener {

	Button back;
	Button login;

	EditText email_input;
	EditText pass_input;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		//
		back = (Button) findViewById(R.id.back);
		login = (Button) findViewById(R.id.login_button);
		email_input = (EditText) findViewById(R.id.email_input);
		pass_input = (EditText) findViewById(R.id.password_input);

		email_input.setText("nghialv.bk@gmail.com");
		pass_input.setText("password");
		// add listener
		back.setOnClickListener(this);
		login.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == back) {
			finish();
		}
		if (v == login) {
			String email = email_input.getText().toString();
			String pass = pass_input.getText().toString();

			if (email.equals("") || pass.equals("")) {// null input
				new AlertDialog.Builder(this).setTitle("")
						.setMessage("no data inputed")
						.setNeutralButton("Close", null).show();
			} else {// login
				String encripted_pass = CocoikoUtils.md5(pass);

				ProgressDialog dialog = ProgressDialog.show(this, "",
						"Login ...");
				String status = ClientUtils.login(this,email, encripted_pass);
				dialog.dismiss();

				if (status == null) {//no message
					Intent intent = new Intent(Login.this, Home.class);

					startActivity(intent);
					finish();
				} else {
					// show login error message
					new AlertDialog.Builder(this).setTitle("")
							.setMessage(status)
							.setNeutralButton("Close", null).show();
				}
			}
		}
	}

}
